<?php

/**
 * @author Martin Pernica
 * @copyright 2009
 */
if($_SESSION['rank'] < 4){
	exit(Lang::getBackGlobal('wrong_permissions'));
}
if(!empty($_GET['id'])){
	if(isset($_POST['backend']) && isset($_POST['frontend'])){
		$update = Registry::get('db')->query("UPDATE {prefix}permissions SET backend = '".$_POST['backend']."', frontend = '".$_POST['frontend']."' WHERE id = '".intval($_GET['id'])."'");
		$log->write(8, 'com_permissions', 'NOW()', 'User edited permissions for component #'.intval($_GET['id']), $_SESSION['id']);
			$event = 'roar.alert(\'Status\', \''.Lang::getBackGlobal('permissions_save').'\');';
			array_push($Roar, $event);
	}
	$query = Registry::get('db')->query("SELECT * FROM {prefix}permissions WHERE id = '".intval($_GET['id'])."'");
	$data = $query->fetch_array();
	echo '<h2>'.Lang::getBackGlobal('permissions_for').$data['component'].'</h2><br />';
	echo '<form action="' . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] .
    '" method="post"><table>'.
    '<tr><td>'.Lang::getBackGlobal('permissions_back').'</td>'.
    '<td><select name="backend" size="1">';
    foreach($ranks as $key => $val){
		if($val != 0 and $val != 1){
            $selected = ((int)$data['backend'] == (int)$val) ? 'selected="selected"' : '';
			echo '<option value="'.$val.'" '.$selected.'>'.$key.'</option>';
		}
	}
	echo '</select></td></tr>'.
	'<tr><td>'.Lang::getBackGlobal('permissions_front').'</td>'.
	'<td><select name="frontend" size="1">';
    foreach($ranks as $key => $val){
    	$selected = ((int)$data['frontend'] == (int)$val) ? 'selected="selected"' : '';
		echo '<option value="'.$val.'" '.$selected.'>'.$key.'</option>';
	}
	echo '</select></td></tr>';
	echo '<tr><td><input type="submit" value="'.SUBMIT.'" /> <input type="button" value="Zpět" onclick="window.location.href=\'index.php?p=permissions\'" /></td></tr>';
	echo '</table></form>';
	
}
else{
echo '<h2>'.Lang::getBackGlobal('permissions_title').'</h2><br />';
echo '<table class="clean_table"><tr><th>'.Lang::getBackGlobal('permissions_component').'</th><th>'.Lang::getBackGlobal('permissions_back').'</th><th>'.Lang::getBackGlobal('permissions_front').'</th><th>'.Lang::getBackGlobal('action').'</th></tr>';
$query = Registry::get('db')->query("SELECT * FROM {prefix}permissions");
while($data = $query->fetch_array()){
	if(is_file('..'.DIR_SEP.'components'.DIR_SEP.$data['component'].DIR_SEP.'info.xml')){

    $xml = simplexml_load_file('..'.DIR_SEP.'components'.DIR_SEP.$data['component'].DIR_SEP.'info.xml');
    $name = (string)$xml->info[0]->name;

	}else{
		$name = $data['component'];
	}
	
	echo '<tr><td>'.$name.'</td><td><strong>';
	echo rank2text($data['backend']);
	echo '</strong></td><td><strong>';
	echo rank2text($data['frontend']);
	echo '</strong></td><td><a href="index.php?p=permissions&id='.$data['id'].'">Upravit</a></td></tr>';
}
echo '</table>';
}
?>